package com.liang.maogouyuyin.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.liang.maogouyuyin.AppConst
import com.liang.maogouyuyin.R
import com.liang.maogouyuyin.databinding.ItemQuotationsBinding
import com.liang.maogouyuyin.utils.PlayerUtil

class QuotationsAdapter(val context: Context): RecyclerView.Adapter<QuotationsAdapter.ViewHolder>() {
    class ViewHolder(binding: ItemQuotationsBinding): RecyclerView.ViewHolder(binding.root) {
        val itemImage = binding.itemQuotationsImage
        val itemText = binding.itemQuotationsText
        val itemPlayer = binding.itemQuotationsPlayer
    }

    private var isDog = false

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemQuotationsBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return 10
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (isDog)
            holder.itemImage.setImageResource(R.drawable.quotations_dog)
        else
            holder.itemImage.setImageResource(R.drawable.quotations_cat)
        holder.itemText.text = context.getString(AppConst.quotationsTextArray[position])
        holder.itemPlayer.setOnClickListener {
            if (isDog)
                PlayerUtil.playAudio("asset:///sound/dog/quotations_index_${position}.mp3")
            else
                PlayerUtil.playAudio("asset:///sound/cat/quotations_index_${position}.mp3")
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setDog(isDog: Boolean) {
        this.isDog = isDog
        notifyDataSetChanged()
    }
}